package com.roy.eureka.server.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.roy.eureka.api.dto.User;

@RestController
@RequestMapping(value="/user")
public class UserController {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	@RequestMapping(value="/getUser")
	public List<User> getUser(@RequestParam int i){
		logger.info("getUser param i= "+i);
		List<User> res = new ArrayList<User>();
		for(int j = 0 ; j <= i ; j ++) {
			User user = new User();
			user.setUserId("No"+j);
			user.setUserName("第"+j+"个用户");
			user.setUserPassword("123456"+j);
			res.add(user);
		}

		return res;
	}
	
	@RequestMapping(value="/getSomeUsers")
	public List<User> getSomeUsers(@RequestBody List<User> users) {
		AtomicInteger userId = new AtomicInteger(1);
		for(User user:users) {
			user.setUserId(""+userId.getAndIncrement());
			user.setUserName("userName set from EurekaServer");
		}
		return users;
	}
	
	@RequestMapping(value="/getOneUser")
	public User getOneUsers(@RequestBody User user) {
		if(StringUtils.isEmpty(user.getUserId())) {
			user.setUserId("No1 vir user");
		}
		user.setUserName("userName set by Server through Eureka");
		return user;
	}
	
	@RequestMapping(value="/getStringInfo")
	public String  getStringInfo(@RequestParam String test) {
		logger.info("getStringInfo test => "+test);
		return test;
	}
	
	@RequestMapping(value="/hello",method= {RequestMethod.GET,RequestMethod.POST})
	public String hello() {
		logger.info("get hello request");
		return "hello";
	}
}
